package com.brillilab.starter.controller.web.instrument;

import com.brillilab.common.entity.ResponseVo;
import com.brillilab.domain.enums.instrument.MaintenanceTypeEnum;
import com.brillilab.domain.po.instrument.Instrument;
import com.brillilab.domain.po.instrument.InstrumentMaintenance;
import com.brillilab.domain.vo.instrument.MaintenanceCreateVo;
import com.brillilab.domain.vo.instrument.MaintenanceVo;
import com.brillilab.domain.vo.instrument.SchedulerEventVo;
import com.brillilab.domain.vo.user.UserInfoVo;
import com.brillilab.service.logic.instrument.InstrumentLogic;
import com.brillilab.service.logic.instrument.MaintenanceLogic;
import com.brillilab.service.logic.instrument.converter.MaintenanceConverter;
import com.brillilab.service.logic.user.UsersLogic;
import com.brillilab.starter.aspect.AspectLog;
import com.brillilab.starter.constant.StarterConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 仪器维护 前端控制器
 * </p>
 *
 * @author WuMenghao
 * @since 2019-06-12
 */
@Slf4j
@RestController
@RequestMapping("/web/instrument/maintenance")
public class MaintenanceController {

    @Resource
    private MaintenanceLogic maintenanceLogic;
    @Resource
    private UsersLogic usersLogic;


    /**
     * 添加维护信息
     *
     * @param create
     * @return
     */
    @AspectLog
    @RequestMapping(value="/add", method=RequestMethod.POST)
    public ResponseVo addMaintenance(@RequestBody MaintenanceCreateVo create,@RequestHeader(value=StarterConstant.TOKEN) String token) {

        // 参数校验
        Assert.isTrue(create!=null && create.getInstrumId()!=null && create.getMaintenances()!=null && create.getMaintenances().size()>0,"请求参数缺失！");

        List<MaintenanceVo> maintenanceVos = create.getMaintenances();

        for (MaintenanceVo maintenanceVo : maintenanceVos) {
            Assert.isTrue( maintenanceVo.getType()!=null && StringUtils.isNotBlank(maintenanceVo.getStartTime())
                    && StringUtils.isNotBlank(maintenanceVo.getEndTime()) && maintenanceVo.getStartDay()!=null && maintenanceVo.getEndDay()!=null,"请求参数缺失！");

            if(maintenanceVo.getType().equals(MaintenanceTypeEnum.YEAR.getValue())){
                Assert.isTrue(maintenanceVo.getStartMonth()!=null && maintenanceVo.getEndDay()!=null,"请求参数缺失！");
            }
        }

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        // 添加维护信息
        maintenanceLogic.addMaintenance(create.getInstrumId(),maintenanceVos,userInfo);

        return ResponseVo.success();

    }

    /**
     * 删除维护信息
     *
     * @param id
     * @return
     */
    @AspectLog
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public ResponseVo deleteMaintenance(Long instrumId,Long id,@RequestHeader(value=StarterConstant.TOKEN) String token) {

        // （1）参数校验
        Assert.isTrue(instrumId!=null && id!=null && StringUtils.isNotBlank(token),"请求参数缺失！");

        UserInfoVo userInfo=usersLogic.getUserByToken(token);

        // （2）删除维护信息
        maintenanceLogic.deleteMaintanence(instrumId,id,userInfo);

        return ResponseVo.success();
    }

    /**
     * 日历插件中显示维护
     *
     * @param instrumId
     * @return
     */
    @RequestMapping(value = "/schedulers", method = RequestMethod.GET)
    public ResponseVo maintenanceEventList(Long instrumId) {

        Assert.notNull(instrumId,"请求参数缺失！");

        List<SchedulerEventVo> schedulerEventVos = maintenanceLogic.getMaintenanceEventList(instrumId);

        return ResponseVo.success(schedulerEventVos);
    }
}
